package four;

public class SquareEquation {
    private double a;
    private double b;
    private double c;

    public SquareEquation(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public void setA(double a) {
        this.a = a;
    }
    public void setB(double b) {
        this.b = b;
    }
    public void setC(double c) {
        this.c = c;
    }

    public String[] calculateRoots() {
        if(a != 0) {
            System.out.println("是一元二次方程");
        }
        double discriminant = b * b - 4 * a * c;
        if (discriminant > 0) {
            double root1_ = (-b + Math.sqrt(discriminant)) / (2 * a);
            double root2_= (-b - Math.sqrt(discriminant)) / (2 * a);
            String root1 = String.format("%.6f", root1_);
            String root2 = String.format("%.6f", root2_);
            return new String[]{root1, root2};
        } else if (discriminant == 0) {
            System.out.println("是一元二次方程");
            double root_ = (-b) / (2 * a);
            String root = String.format("%.6f", root_);
            return new String[]{root};
        } else {
            return null;
        }
    }
}
